package edu.spangler.dang.FriendViewer;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

/**
 * Class that allows for adding "pins" to a map
 * @author Tuan Dang
 */
public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
	
	private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
	private Context mapContext;
	@SuppressWarnings("unused")
	private String username;
	
	/**
	 * Default constructor
	 * @param defaultMarker the object to draw icons with
	 * @param context the map's context
	 * @param user a username
	 */
	public ItemizedOverlay(Drawable defaultMarker, Context context, String user) {
		super(boundCenterBottom(defaultMarker));
		mapContext = context;
		username = user;
	}
	
	/**
	 * Method that returns the number of overlay objects
	 */
	public int size() {
		return mapOverlays.size();
	}
	
	/**
	 * Method to create items
	 */
	public OverlayItem createItem(int i) {
		return mapOverlays.get(i);
	}
	
	/**
	 * Method that adds an item to the list as well
	 * as processing the new item.
	 * @param item
	 */
	public void addOverlay(OverlayItem item) {
		mapOverlays.add(item);
		populate();
	}
	
	/**
	 * Method that handles event where a user clicks on one
	 * of the pins on the map. Will show a contact screen displaying
	 * more information about the user.
	 */
	protected boolean onTap(int idx) {
		OverlayItem item = mapOverlays.get(idx);
		Intent myIntent = new Intent(mapContext, ViewContact.class);
		myIntent.putExtra("username", item.getTitle());
		GeoPoint g = item.getPoint();
		myIntent.putExtra("latitude", g.getLatitudeE6()/1.0E6);
		myIntent.putExtra("longitude", g.getLongitudeE6()/1.0E6);
		mapContext.startActivity(myIntent);
		return true;
	}
}
